package com.kun;

import com.kun.desensitized.handler.DesensitizedHandler;
import com.kun.ext.DesensitizedInterceptor;
import org.springframework.context.annotation.Bean;

import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;

public class MybatisExtConfig {

    @Bean
    public MybatisInnerResultInterceptor mybatisInnerResultInterceptor() {
        MybatisInnerResultInterceptor mybatisInnerResultInterceptor = new MybatisInnerResultInterceptor();
        DesensitizedInterceptor customInterceptor = new DesensitizedInterceptor();
        customInterceptor.setDesensitizedHandler(desensitizedHandlerList());
        mybatisInnerResultInterceptor.setCustomInterceptor(customInterceptor);
        return mybatisInnerResultInterceptor;
    }

    public List<DesensitizedHandler> desensitizedHandlerList() {
        List<DesensitizedHandler> desensitizedHandlerList = new ArrayList<>();
        ServiceLoader<DesensitizedHandler> desensitizedHandlers = ServiceLoader.load(DesensitizedHandler.class);
        desensitizedHandlers.forEach(desensitizedHandlerList::add);
        return desensitizedHandlerList;
    }

}
